window.query_cache = {};
$("#search-input").typeahead({hint: true, highlight: true, minLength: 1, source: function (e, t) {
    if (query_cache[e]) {
        t(query_cache[e]);
        return
    }
    if (typeof searching != "undefined") {
        clearTimeout(searching);
        t([]);
        $(".loading_absolute").show()
    }
    searching = setTimeout(function () {
        return $.getJSON("/raovat/typeahead", {q: e}, function (n) {
            var r = [];
            $.each(n, function () {
                r.push(this.title)
            });
            query_cache[e] = r;
            $(".loading_absolute").hide();
            return t(r)
        })
    }, 3)
}});
$("#manage_topic_search").typeahead({hint: true, highlight: true, minLength: 1, source: function (e, t) {
    if (query_cache[e]) {
        t(query_cache[e]);
        return
    }
    if (typeof searching != "undefined") {
        clearTimeout(searching);
        t([]);
        $(".loading_absolute").show()
    }
    searching = setTimeout(function () {
        return $.getJSON("/raovat/typeahead-user-topic", {q: e}, function (n) {
            var r = [];
            $.each(n, function () {
                r.push(this.title)
            });
            query_cache[e] = r;
            $(".loading_absolute").hide();
            return t(r)
        })
    }, 3)
}});
$(".richtext").wysihtml5({html: true, link: false, image: true});
$(function () {
    $.resetFormOnFocus();
    $.validateForm();
    $(".timepicker").datepicker({yearRange: "1950:", changeMonth: true, changeYear: true, dateFormat: "yy-m-dd"})
});
$.resetFormOnFocus = function () {
    $("form.require-validate input").keypress(function () {
        if ($(this).next("span.error-field").length > 0)$(this).next("span.error-field").remove()
    })
};
$.resetForm = function (e) {
    $("#filelist").html("");
    $("form[name=" + e + "] input:not(input[type=hidden], input[type=submit])").each(function () {
        $(this).val("")
    });
    $("form[name=" + e + "] textarea").each(function () {
        $(this).val("");
        $(this).html("")
    })
};
$.validateForm = function (e) {
    $("form.require-validate").submit(function () {
        $(".loading").show();
        var e = $(this).attr("name");
        var t = $(this).attr("id");
        var n = true;
        $(this).find(".group_validate").each(function () {
            if ($(this).find("input").attr("req") == "true") {
                if ($(this).find("input").val() == "") {
                    if (n)n = false;
                    if (!$(this).find("input").hasClass("alert alert-danger"))$(this).find("input").addClass("alert alert-danger");
                    $(".overlay-loading").removeClass("alert-info").addClass("alert-danger").show().text("Vui lòng điền đầy đủ thông tin");
                    $(".loading").hide()
                }
            }
            if ($(this).find("input").hasClass("captcha_require")) {
                var e = $("#captcha").text();
                var t = $("#captcha_txt").val();
                if (e != t) {
                    getCaptcha();
                    if (n)n = false;
                    if (!($(this).find("input").next("span.error-field").length > 0))$(this).find("button").after('<br/><span class="error-field"><i class="mini-ico-exclamation-sign"></i> <b style="color: #ff0000">Mã xác nhận sai!</b></span><br/>')
                }
            }
        });
        $(".loading").hide();
        if (!n)return false; else {
            if ($(this).hasClass("ajax-form")) {
                if (!($(this).find("input[name=ajax_request]").length > 0))$(this).append('<input type="hidden" name="ajax_request" value = "1">');
                $.ajax({
				url: $(this).attr("action"), type: "POST", data: $(this).serialize(), dataType: "html", beforeSend: function () {
                    $(".overlay-loading").fadeIn(500)
                }, success: function (t) {
                    t = JSON.parse(t);
                    if (t.result == true) {
                        $.resetForm(e);
                        jsuccess(t.message);
                        $("#modal_reg").modal("hide")
                    } else {
                        jerror(t.message)
                    }
                    if (t.redirect) {
                        window.location.href = t.url
                    }
                }, error: function () {
                    jerror("Request timeout");
                    $(".overlay-loading").fadeOut(1e3)
                }});
                $(".loading").hide();
                return false
            }
            if ($(this).hasClass("ajax-login")) {
                if (!($(this).find("input[name=ajax_request]").length > 0))$(this).append('<input type="hidden" name="ajax_request" value = "1">');
                $.ajax({url: $(this).attr("action"), type: "POST", data: $(this).serialize(), dataType: "html", beforeSend: function () {
                    $(".overlay-loading").fadeIn(500)
                }, success: function (t) {
                    t = JSON.parse(t);
                    $("#modal_login").modal("hide");
                    if (t.result == true) {
                        console.log(t.user_info.name);
                        $.resetForm(e);
                        jsuccess(t.message);
                        var n = '<li><a href="/raovat/dangtin" >Đăng tin</a></li>' + '<li class="dropdown">' + '<a class="dropdown-toggle" data-toggle="dropdown"  href="#" id="user_group">' + '<i class="glyphicon glyphicon-user"></i><span class="hidden-phone"></span>' + t.user_info.name + ' <span class="caret"></span> ' + "</a>" + ' <ul class="dropdown-menu" aria-labelledby="user_group">' + ' <li><a class="font_black" href="/user/topic">Quản lý rao vặt</a></li>' + ' <li class="divider"></li> ' + ' <li><a class="font_black" href="/user/profile">Quản lý tài khoản</a></li>' + ' <li class="divider"></li>' + '  <li><a class="font_black" href="/user/logout">Thoát</a></li> ' + " </ul> " + " </li>";
                        $("#nav-user").html("");
                        $("#field-userinfo").remove();
                        $("#nav-user").append(n)
                    } else {
                        jerror(t.message)
                    }
                    if (t.redirect) {
                        window.location.href = t.url
                    }
                }, error: function () {
                    jerror("Request timeout");
                    $(".overlay-loading").fadeOut(1e3)
                }});
                $(".loading").hide();
                return false
            }
        }
        $(this).find(".loading").hide()
    });
    return false
};
jsuccess = function (e) {
    jSuccess(e, {autoHide: true, clickOverlay: false, MinWidth: 250, TimeShown: 3e3, ShowTimeEffect: 200, HideTimeEffect: 200, LongTrip: 20, HorizontalPosition: "center", VerticalPosition: "top", ShowOverlay: true, ColorOverlay: "#FFF", OpacityOverlay: .3, onClosed: function () {
    }, onCompleted: function () {
    }})
};
jerror = function (e) {
    jError(e, {autoHide: true, clickOverlay: false, MinWidth: 250, TimeShown: 3e3, ShowTimeEffect: 200, HideTimeEffect: 200, LongTrip: 20, HorizontalPosition: "center", VerticalPosition: "top", ShowOverlay: true, ColorOverlay: "#FFF", OpacityOverlay: .3, onClosed: function () {
    }, onCompleted: function () {
    }})
};
jnotify = function (e) {
    jNotify(e, {autoHide: true, clickOverlay: false, MinWidth: 250, TimeShown: 3e3, ShowTimeEffect: 200, HideTimeEffect: 200, LongTrip: 20, HorizontalPosition: "center", VerticalPosition: "top", ShowOverlay: true, ColorOverlay: "#FFF", OpacityOverlay: .3, onClosed: function () {
    }, onCompleted: function () {
    }})
}